<template>
	<el-row type="flex" class="row-bg" justify="center">
			<el-col :xl="6" :lg="7">
			<h1><i class="el-icon-user-solid"></i><font color='green'>姚镇群</font></h1>
		 <h1><i class="el-icon-phone"></i><font color='green'>羊肉下单电话:13534529333</font></h1>
<h1><i class="el-icon-user-solid"></i><font color='green'>姚镇雄</font></h1>
		<h1><i class="el-icon-phone"></i><font color='green'>羊肉下单电话:13682752678</font></h1>
		<h1><i class="el-icon-user-solid"></i><font color='green'>姚东平</font></h1>
		<h1><i class="el-icon-phone"></i><font color='green'>水果下单电话:13421624216</font></h1>
		</el-col>
		<el-col :xl="6" :lg="7">
			<h1><font color='green'>姚记羊肉水果</font></h1>
			<el-image :src="require('@/assets/m.jpg')" style="height: 370px; width: 270px;"></el-image>

		</el-col>
	
		<el-col :span="1">
			<el-divider direction="vertical"></el-divider>
		</el-col>
		<el-col :xl="6" :lg="7">
			<el-form :model="loginForm" :rules="rules" ref="loginForm" label-width="80px">
				<el-form-item   prop="username" style="width: 380px;">
				 <span slot="label">
      	<span style="color: black">账号</span>
      </span>
					<el-input  style="width: 300px" v-model="loginForm.username"></el-input>
					
				</el-form-item>
				<el-form-item  prop="password"  style="width: 380px;">
				 <span slot="label">
      	<span style="color: black">密码</span>
      </span>
					<el-input style="width: 300px" v-model="loginForm.password" type="password"></el-input>
				</el-form-item>
				<el-form-item  prop="code"  style="width: 380px;">
				 <span slot="label">
      	<span style="color: black">验证码</span>
      </span>
					<el-input v-model="loginForm.code"  style="width: 172px; float: left" maxlength="5"></el-input>
					<el-image :src="captchaImg" class="captchaImg" @click="getCaptcha"></el-image>
				</el-form-item>
				<el-form-item>
					<el-button type="success" @click="login('loginForm')">登陆</el-button>
					<el-button @click="resetForm('loginForm')" type="primary" >重置</el-button>
					
				</el-form-item>
			</el-form>
		</el-col>
	</el-row>

</template>

<script>

	import qs from 'qs'

	export default {
		name: "Login",
		data() {
			return {
				loginForm: {
					username: '',
					password: '',
					code: '',
					token: ''
				},
				rules: {
					username: [
						{ required: true, message: '请输入用户名', trigger: 'blur' }
					],
					password: [
						{ required: true, message: '请输入密码', trigger: 'blur' }
					],
					code: [
						{ required: true, message: '请输入验证码', trigger: 'blur' },
						{ min: 5, max: 5, message: '长度为 5 个字符', trigger: 'blur' }
					],
				},
				captchaImg: null
			};
		},
		methods: {
			login(formName) {
				this.$refs[formName].validate((valid) => {
					if (valid) {
						this.$axios.post('/login?'+ qs.stringify(this.loginForm)).then(res => {

							console.log(res)

							const jwt = res.headers['authorization']
                            console.log("jwt信息")
							console.log(jwt)
							this.$store.commit('SET_TOKEN', jwt)
							this.$router.push("/index")
							
						})

					} else {
						console.log('error submit!!');
						return false;
					}
				});
			},
			resetForm(formName) {
				this.getCaptcha()
				this.$refs[formName].validate((valid) => {
					if (valid) {
						
                   this.$refs[formName].resetFields();
					} else {
						console.log('error submit!!');
						return false;
					}
				});
			},
			getCaptcha() {
				this.$axios.get('/captcha').then(res => {

					console.log("/captcha")
					console.log(res)
					this.loginForm.token = res.data.data.token
					this.captchaImg = res.data.data.captchaImg
					this.loginForm.code = ''
				})
			}
		},
		created() {
			this.getCaptcha()
		}
	}
</script>

<style scoped>

	.el-row {
		
		display: flex;
		align-items: center;
		text-align: center;
		justify-content: center;
		background: url("../assets/435.jpg") center no-repeat ;
		background-position: center;
        height: 100%;
        width: 100%;
        background-size: cover;
        position: fixed;
	}

	.el-divider {
		height: 500px;
	}
    
	.captchaImg {
		float: left;
		margin-left: 8px;
		border-radius: 4px;
	}
	.text {
    font-size: 14px;
  }

  .item {
    padding: 18px 0;
  }

  .box-card {
    width: 480px;
  }

</style>